import {Cache} from '../../common/utils/Common';

export default {
    props: {
        id: {
            type: String
        },
        cover: {
            type: String
        },
        title: {
            type: String
        },
        singer: {
            type: String
        },
        desc: {
            type: String
        },
        singAndDesc: {
            type: Boolean,
            default: false
        },
        play: {
            type: Boolean,
            default: true
        },
        playing: {
            type: Boolean,
            default: false
        }
    },
    /**
     * 点击播放音乐
     */
    playMusic() {
        let cache = this.$app.$def.cache;
        if (!cache) {
            cache = new Cache();
            this.$app.$def.cache = cache;
        }
        cache.set(this.id, this.getCurrentPlay());
        // 1. 点击卡片播放，将音乐信息传给父组件
        this.$emit('playMusic', this.getCurrentPlay());
        // 2. 将当前播放音乐放入缓存
        cache.set('currentPlay', this.getCurrentPlay());
    },
    stopMusic() {
        this.$emit('stopMusic', this.getCurrentPlay());
        let currentPlay = this.$app.$def.cache.get('currentPlay');
        if (currentPlay) {
            currentPlay.playing = false;
            this.$app.$def.cache.set('currentPlay', currentPlay);
        }
    },
    getCurrentPlay() {
        return {id: this.id, cover: this.cover, title: this.title, singer: this.singer, desc: this.desc, play: this.play, playing: this.playing };
    },
    setCurrentPlay(currentPlay) {
        this.id = currentPlay.id;
        this.cover = currentPlay.cover;
        this.title = currentPlay.title;
        this.singer = currentPlay.singer;
        this.desc = currentPlay.desc;
        this.play = currentPlay.play;
        this.playing = currentPlay.playing;
    }
}